package club.cearnach.api.security.handler;

import club.cearnach.api.vo.LoginResultVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 阮胜
 * @date 2018/5/23 14:11
 */
@Component
@Slf4j
public class MovieAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        log.info("登录成功");
        PrintWriter writer = response.getWriter();
        LoginResultVO loginResultVO = new LoginResultVO(true);
        loginResultVO.setPrivilege(authentication.getAuthorities().iterator().next().getAuthority().replace("ROLE_", ""));
        writer.write(JSONObject.toJSONString(loginResultVO));
        writer.close();
    }
}
